package cn.micro.keep.accounts.controller;


import cn.micro.keep.accounts.api.Result;
import cn.micro.keep.accounts.entity.AccountOdsRecordImg;
import cn.micro.keep.accounts.service.AccountOdsRecordImgService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 账单图片信息表 前端控制器
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-08
 */
@RestController
@RequestMapping("/accounts/accountOdsRecordImg")
@RequiredArgsConstructor
public class AccountOdsRecordImgController {

    private final AccountOdsRecordImgService recordImgService;


    /**
     * 上传账单备案文件
     * @param files
     * @return
     */
    @PostMapping("uploadRecordImg")
    public Object uploadRecordImg(MultipartFile[] files){
        if (!recordImgService.uploadRecordImg(files)){
            return new Result<>().fail("上传失败");
        }
        return new Result<>().success("上传成功");
    }

    /**
     * 删除账单备案文件
     *
     * @return
     */
    @DeleteMapping("removeRecordImg/{imgId}")
    public Object removeRecordImg(@PathVariable Long imgId){
        if (!recordImgService.removeRecordImg(imgId)){
            return new Result<>().fail("删除失败");
        }
        return new Result<>().success("删除成功");
    }

    /**
     * 显示记录视图
     *
     */
    @GetMapping("viewRecordImg")
    public void viewRecordImg(@PathVariable Long imgId, HttpServletRequest request, HttpServletResponse response){
        try {
            recordImgService.viewRecordImg(imgId, request, response);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("图片显示失败");
        }
    }
}
